/**
 * Created by bgy on 2016/12/6.
 * 我的水果
 */
import React from 'react';
import {Link} from 'react-router';

class GoodsQualityRecordItemComponent extends React.Component {
  goToRecordPage(){
    if(this.props.item && this.props.item.status){
      switch(this.props.item.status){
        case 'A':    //质检通过，跳往查看质检报告界面
        case 'C':    //质检未通过，跳往查看质检报告界面
              this.props.handleCheckQuality(this.props.item,this.props.item.status);
              break;
      }
    }
    console.log('跳往质检页面---质检参数为'+this.props.item.qualityRecordId+'==='+this.props.item.status);
  }
  render(){
    var imgSrc=this.props.item.status=='A'?'images/myFruit/icon-pass-label@2x.png':'images/myFruit/icon-notpass@2x.png';
    return(
      <li>
        <div className='record-item'>
          <span>{this.props.item.applyDate}</span>
          <span className='item-status'> <img src={imgSrc} width='45'/></span>
          <span className='item-handle' onClick={this.goToRecordPage.bind(this)}>查看 > </span>
        </div>
      </li>
    )
  }
}


class GoodsQualityRecordComponent extends React.Component {

  constructor() {
    super();
    this.handleCheckQuality = this.handleCheckQuality.bind(this);
    this.state = {
      //测试数据
      qualityRecordList: [
        {
          qualityRecordId:'1',
          qualityRecordNo:'000001',
          vendorGoodsId:'1-1',
          status:'A',
          qualityPlatId:'001',
          applyDate:'2016-09-01',
          type:'A'
        },
        {
          qualityRecordId:'2',
          qualityRecordNo:'000002',
          vendorGoodsId:'1-3',
          status:'C',
          qualityPlatId:'002',
          applyDate:'2016-09-03',
          type:'B'
        }
      ]
    }
  }
  componentWillReceiveProps() {
    console.info(this.props['data-vendorGoodsId']);
    if(this.props['data-vendorGoodsId']){
      //$.ajax(
      //  {
      //    'url': this.props.config.baseUrl + '/quality/queryQualityRecordList',
      //    type: 'post',
      //    dataType: 'JSON',
      //    data: JSON.stringify({
      //      vendorGoodsId: this.props['data-vendorGoodsId'],
      //      vendorId: window.localStorage.getItem('vendorInfo').vendorId
      //    }),
      //    success: function (data) {
      //      if (data.errorCode == 0) {
      //        this.setState({qualityRecordList: data.data});
      //      }
      //    }.bind(this),
      //    error: function (data) {
      //      return data;
      //    }
      //  }
      //);
    }

  }

  handleCheckQuality(recordItem,recordStatus){
    this.props.handleCheckQuality(recordItem,recordStatus);
  }

  render() {
    var handleCheckQuality = this.handleCheckQuality;
    var recordList=[];
    if(this.state.qualityRecordList && this.state.qualityRecordList.length>0){
      return (
        <div className='record-box'>
          <ul>
            {
              this.state.qualityRecordList.map(function (item,index) {
                return (
                  <GoodsQualityRecordItemComponent item={item} key={item.qualityRecordId} handleCheckQuality={handleCheckQuality}/>
                );
              })
            }
          </ul>
        </div>
      )
    }else{
      return(<div>暂无质检记录</div>)
    }
    //return (
    //  <section className='record-box'>
    //    <ul>
    //      {recordList}
    //    </ul>
    //  </section>
    //)
  }

}

export default GoodsQualityRecordComponent;
